package com.herui.iot.data.user;


import com.alibaba.fastjson.TypeReference;
import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.JsonUtils;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.data.eval.EvalHostoryInfo;
import com.herui.sport_care.request.SportPageResponse;
import com.herui.sport_care.request.SportResponse;

import java.util.List;

/**
 * Created by amanda.shan on 2021/10/19.
 */
public abstract class BasePageCallBack<T> extends Callback.CommonCallback<String> {

    private Callback.CommonCallback<T> callback;

    public BasePageCallBack(Callback.CommonCallback<T> callback) {
        this.callback = callback;
    }

    @Override
    public void onSuccess(String s) {

        LogUtils.d(s);
        SportPageResponse<T> apiResponse;
        try {
            apiResponse = getApiResponse(s);
            if (apiResponse.getSuccess()) {
                T t = JsonUtils.parseObject(apiResponse.getResult().getContent().toString(),
                        new TypeReference<T>() {
                        });

                callback.onSuccess((T) apiResponse.getResult().getContent());
                onResult((T) apiResponse.getResult().getContent());
            } else {
                callback.onError(null, "%s", apiResponse.getMsg());
            }
        } catch (Exception e) {
            callback.onError(e, "", "");
            e.printStackTrace();
        }
    }

    public abstract SportPageResponse<T> getApiResponse(String s) throws Exception;

    public void onResult(T t){

    }

    @Override
    public void onError(Throwable throwable, String s, String s1) {
        if (s == null) {
            s = throwable.getMessage();
        }

        callback.onError(throwable, s + ":%s", s1);
    }
}
